clear
clear mata
capture clear matrix
set mem 2g		//Arbeitsspeicher erhöhen
set maxvar 10000	//Anzahl der zuläs. Variablen erhöhen
set matsize 5000

*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
* SET GLOBAL $PATHS
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
global root = "INSERT CUSTOMIZED PATH"
global data= "$root\data"
global tables	= "$root\figsandtabs"
global figures	= "$root\figsandtabs"
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

*version 13		
// Version festlegen
set more off		
// Bildschirmausgabe steuern

set scrollbufsize 50000

*#delimit ;

set more off		
set scrollbufsize 50000
use "$data\temp\SOEP1984_2016", clear


label variable cid "Enrollee ID"
label variable syear "Year"
label variable female "Female"
label variable labgro "Self-reported individual gross wages in euro of given year, SOEP"
label variable labnet "Self-reported individual net wages in euro of given year, SOEP"
label variable o_indtotal "Individual total income in euro of given year, SOEP"
label variable o_eqpost "Equivalized post-tax post-transfer income in euro of given year, SOEP"
*label variable premium "Self-reported monthly premium in euro of given year, original SOEP respondents"
*label variable state "State of residencs, SOEP respondents"

label variable oecd "OECD equivalent score (considering household members)"
label variable deflator "Deflator to inflate moneary euro values to 2016 dollars"
label variable labgroII "Labgro but inflated to 2016 dollars"
label variable labnetII "Labnet but inflated to 2016 dollars"
label variable o_eqpostII "o_eqpostII but inflated to 2016 dollars"
label variable o_indtotalII "o_indtotalII but inflated to 2016 dollars"

label variable noschool "No school degree"
label variable dropout "Dropout of high school"
label variable Hauptschule "Degree after 8/9 years of schooling"
label variable Realschule "Degree after 10 years of schooling"
label variable Fachhochschule "Degree after 12 years of schooling"
label variable Abi "Degree after 13 years of schooling"

label variable age "Age of respondent"
label variable civilservant "Binary, 1 if currently employed as civil servant"
label variable selfemployed "Binary, 1 if currently self-employed"
label variable whitecollar "Binary, 1 if currently employed as white collar"
label variable hospitalstay "Binary, 1 if hospital stay in past calendar year"
label variable hospitalnights "Number of hospital nights in past calendar year"
label variable doctorvisits "Number of doctor visist in past 3 months (interviews in first quarter of year)"
label variable PrivateHI "If privately insured"
label variable PrivateHIfamilyenrollee "If private insured through family coverage"
label variable PrivateHIdeductible "Deductible of private insurance policy (not asked before 1997)"
label variable PrivateHISingleCoverage "If private insurance is single coverage"
label variable PrivateHINoCovered "Number of people covered under policy"
label variable PublicSector  "Binary, 1 if currently employed in public sector"
label variable fulltimeemployed "Binary, 1 if currently full-time employed "
label variable parttimeemployed  "Binary, 1 if currently part-time employed"
label variable jobtraining "Binary, 1 if currently in job training"

drop age
gen age=syear-ple0010
drop if age==.



***********************************************
***SUMMARY STATISTIC SOEP OVERALL: TABLE A1 ***
***********************************************


global xvar "female age noschool dropout Hauptschule Realschule Abi civilservant selfemployed whitecollar fulltimeemployed parttimeemployed labgroII labnetII o_indtotalII o_eqpostII  hospitalnights doctorvisits PrivateHI" 

gen balanced=1 if !missing(age, female, noschool, dropout, Hauptschule, Realschule, Abi, civilservant, selfemployed, whitecollar, fulltimeemployed, parttimeemployed, o_indtotalII, o_eqpostII)

tab balanced, miss
drop if balanced!=1


cd "$tables"

sum $xvar
sutex $xvar, nobs labels minmax digits(4) title(Descriptive Statistics) key(DesStat) longtable file("summary-stats_SOEP.tex") replace


*********************************************
***SAMPLE SELECTION FOR SWITCHING ANALYSIS***
*********************************************


drop if syear<2004
drop if syear==2005
drop if syear==2007

*********************************
********DATA GENERATION**********
*********************************

tab m11126 if syear==2011
tab m11124 if syear==2011
sum m11104 m11105 m11106 m11107 m11108 m11109 if syear==2011
sum m11113 m11115 m11120 if syear==2011

rename m11122 height
rename m11123 weight
rename m11124 disabled
rename m11126 SAH
sum m11127
rename m11127 outpatient_visits

replace ple0081=. if ple0081==-8
sum ple0081 if syear==2016
tab ple0081
recode ple0081(2=0), gen(smoker)

tab ple0043
recode ple0043 (2=0), gen(longterm_sick)
sum ple0032 ple0031 ple0033 ple0034 if syear==2015
sum ple0032 ple0031 ple0033 ple0034 if syear==2011
tab ple0032 if syear==2016

recode ple0031 (3/5=0) (1/2=1) (-8=.), gen(physical_less)
tab physical_less
recode ple0032 (3/5=0) (1/2=1) (-8=.), gen(physical_limits)
recode ple0033 (3/5=0) (1/2=1) (-8=.), gen(emotional_less)
recode ple0034 (3/5=0) (1/2=1) (-8=.), gen(emotional_limit)


tab ple0095 if syear==2011
tab ple0096 if syear==2011
tab ple0102 if syear==2011


tab SHI, miss

tab ple0102
cap gen PrivateHIpolicyholder=1 if ple0102==2

cap gen PrivateHIfamilyenrollee=1 if ple0102==1
tab ple0099, miss

cap gen SHIfamilyenrollee=1 if ple0099==3
by syear: sum SHIfamilyenrollee
sort syear
by syear: sum PrivateHIpolicyholder
by syear: sum PrivateHIfamilyenrollee
*
tab ple0107
tab ple0108
tab ple0109
tab ple0113
cap gen PrivateHIdeductible=ple0107
*
tab ple0136
cap gen PrivateHIpremium=ple0136
*
tab ple0138
tab ple0139, miss
cap gen PrivateHISingleCoverage=1 if ple0138==1
cap gen PrivateHINoCovered=ple0139
*all years available

sort syear
by syear: sum  PrivateHISingleCoverage
by syear: sum PrivateHIpolicyholder
tab PrivateHI if syear==2016
sum PrivateHI [aw=w11105]

**

cap gen sickdays= ple0046
replace sickdays=0 if ple0044==1
sum sickdays

sum height
replace height=height/100
sum height
gen BMI=weight/(height^2)
sum BMI

cap gen PHI_white=1 if PrivateHI==1 & whitecollar==1
cap gen PHI_self=1 if PrivateHI==1 & selfemployed==1
cap gen PHI_civil=1 if PrivateHI==1 & civilservant==1
cap gen PHI_nowork=1 if PrivateHI==1 & nonemployed==1


***Risks***

sum plh0197 plh0198 plh0199 plh0200 plh0201 plh0202 plh0203 plh0204 plm0254

cap gen risktolerance = plh0204
replace risktolerance=. if risktolerance==-8

cap gen riskhealth = plh0201
replace riskhealth=. if riskhealth==-8

global xvar "age female SAH smoker BMI physical_less physical_limits emotional_less emotional_limit civilservant selfemployed whitecollar hospitalstay hospitalnights doctorvisits PublicSector fulltimeemployed parttimeemployed nonemployed dropout Abi labgro labnet o_eqpost risktolerance"

tabstat $xvar, stats(mean sd)  columns(statistics) f(%9.3fc) 
matrix DesStat = r(StatTotal)'


********************************************************************************************************
***************************************SUMMARY STATISTICS BY GROUP**************************************
********************************************************************************************************


global xvar "age female smoker BMI physical_less physical_limits emotional_less emotional_limit hospitalstay hospitalnights doctorvisits PublicSector fulltimeemployed parttimeemployed dropout Abi labgroII labnetII o_eqpostII risktolerance riskhealth" 

global xvarII "age female smoker BMI hospitalstay hospitalnights doctorvisits fulltimeemployed parttimeemployed dropout Abi o_eqpostII risktolerance"
sum $xvarII

gen balancedII=1 if !missing(age, female, hospitalstay, hospitalnights, doctorvisits, fulltimeemployed, parttimeemployed, dropout, Abi, o_eqpostII, risktolerance)
tab balancedII, miss
drop if balancedII!=1


***********************
****Sample Selection***
***********************

tab PrivateHI, miss
tab SHI, miss

drop if PrivateHIfamilyenrollee==1
drop if SHIfamilyenrollee==1

gen GKV_PKV=0 if SHI==1
replace GKV_PKV=1 if PrivateHI==1
tab GKV_PKV, miss
tab GKV_PKV [aw= w11105]

drop if GKV_PKV==.

*********************

sum $xvar

*estpost tabstat $xvar, stats(mean sd)  columns(statistics)
*esttab using Table1.rtf, cells("mean sd min max") title("Table 1. Summary Statistics") nonumber noobs label replace

sum $xvar if SHI==1 [aw= w11105]
sum $xvar if PrivateHI==1 [aw= w11105]
sum $xvar if PHI_civil==1 [aw= w11105]
sum $xvar if PHI_white==1 [aw= w11105]
sum $xvar if PHI_self==1 [aw= w11105]
sum $xvar if PHI_nowork==1 [aw= w11105]

*168,911
*26,858
*7,808
*5,487
*8,142

gen N= 15714 if SHI==1
replace N= 25997 if PrivateHI==1
replace N= 7576 if PHI_civil==1
replace N= 4887 if PHI_white==1
replace N= 5266 if PHI_self==1
replace N= 7938 if PHI_nowork==1

tabstat $xvar N if SHI==1 [aw=w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT1 = r(StatTotal)'
matrix list DesStatT1

tabstat $xvar N if PrivateHI==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT1b = r(StatTotal)'
matrix list DesStatT1b

tabstat $xvar N if PHI_civil==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT2 = r(StatTotal)'
matrix list DesStatT2

tabstat $xvar N if PHI_white==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT3 = r(StatTotal)'
matrix list DesStatT3

tabstat $xvar N if PHI_self==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT4 = r(StatTotal)'
matrix list DesStatT4

tabstat $xvar N if PHI_nowork==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT5 = r(StatTotal)'
matrix list DesStatT5

matrix define DesStatTall=(DesStatT1, DesStatT1b, DesStatT2, DesStatT3, DesStatT4, DesStatT5)
matrix list DesStatTall

*outtable using "$tables\DesStatTable", mat(DesStatTall) replace caption("Characterization of SHI and PHI Enrollees") clabel(DesStatTall) longtable f(%20.3fc) 



*********************************
***FIGURE A13a: RISK TOLERANCE***
*********************************

hist risktolerance, by(GKV_PKV) discrete
graph export "$figures\risk_tolerance.pdf", as(pdf) replace



**********************
****NOW JUST 25-35****
**********************

preserve

drop if age<25
drop if age>35


sum $xvar if SHI==1 [aw= w11105]
sum $xvar if PrivateHI==1 [aw= w11105]
sum $xvar if PHI_civil==1 [aw= w11105]
sum $xvar if PHI_white==1 [aw= w11105]
sum $xvar if PHI_self==1 [aw= w11105]
sum $xvar if PHI_nowork==1 [aw= w11105]


sum $xvar

drop N
gen N= 23250 if SHI==1
replace N= 2282 if PrivateHI==1
replace N=997 if PHI_civil==1
replace N=465 if PHI_white==1
replace N=503 if PHI_self==1
replace N=262 if PHI_nowork==1

tabstat $xvar N if SHI==1 [aw=w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT1 = r(StatTotal)'
matrix list DesStatT1

tabstat $xvar N if PrivateHI==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT1b = r(StatTotal)'
matrix list DesStatT1b

tabstat $xvar N if PHI_civil==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT2 = r(StatTotal)'
matrix list DesStatT2

tabstat $xvar N if PHI_white==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT3 = r(StatTotal)'
matrix list DesStatT3

tabstat $xvar N if PHI_self==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT4 = r(StatTotal)'
matrix list DesStatT4

tabstat $xvar N if PHI_nowork==1 [aw= w11105], stats(mean)  columns(statistics) save f(%9.3fc) 
matrix DesStatT5 = r(StatTotal)'
matrix list DesStatT5

matrix define DesStatTall=(DesStatT1, DesStatT1b, DesStatT2, DesStatT3, DesStatT4, DesStatT5)
matrix list DesStatTall

outtable using "$tables\DesStatTable25_35", mat(DesStatTall) replace caption("Characterization of SHI and PHI Enrollees") clabel(DesStatTall) longtable f(%20.3fc) 



*********************************
***FIGURE A13b: RISK TOLERANCE***
*********************************

hist risktolerance, by(GKV_PKV) discrete
graph export "$figures\risktolerance25_35.pdf", as(pdf) replace

restore

***


******************************************************
***Generate switching rates by age and over time***
*****************************************************


***define toprivate and topublic switching rate***

tsset pid syear

gen toprivate = 1 if PrivateHI==1 & L.SHI==1 
gen topublic = 1 if SHI==1 & L.PrivateHI==1 

*exclude temporary switchers

replace toprivate =. if F.SHI==1 & PrivateHI==1 & L.SHI==1 
replace toprivate =. if F2.SHI==1 & F1.PrivateHI==1 & PrivateHI==1 & L.SHI==1 

replace topublic =. if F.PrivateHI==1 & SHI==1 & L.PrivateHI==1 
replace topublic =. if F1.SHI==1 & PrivateHI==1 & L1.SHI==1 
replace topublic =. if F2.PrivateHI==1 & F1.SHI==1 & SHI==1 & L.PrivateHI==1 
replace topublic =. if F2.SHI==1 & F1.PrivateHI==1 & PrivateHI==1 & L.SHI==1 
*(0 real changes made)


replace topublic =0 if topublic==.


*here we put the first observed interview to a missing if SHI insured because we cannot exclude that it is topublic although very unlikely*
sort pid syear
by pid: gen interviewno = sum(female<.)
replace topublic =. if interviewno==1 & SHI==1
drop interviewno

**

gen year_dec = floor((syear-1984)/5)
*table syear, c(mean topublic)

summ topublic if syear>1993 & age<=55
summ topublic if syear>1993 & age>55
gen after55= age>55


***************
***FIGURE A1***
***************

sort age
by age: sum topublic [aw=w11103]

preserve
tsset pid syear
keep if PrivateHI==1 | L1.PrivateHI==1

sum topublic
sort age
by age: sum topublic [aw=w11103]

lpoly topublic age if age>=25 & age<=95 & syear>1990, noscatter ci ytitle("Switching Rate from GLTHI to SHI") xlabel(25(10)95) ylabel(0(0.05)0.20)  title("") scheme(s1mono)
graph export "$figures\switching_to_public_lpolyII.pdf", as(pdf) replace

*              storage   display    value
*variable name   type    format     label      variable label
*-------------------------------------------------------------------------
*w11101          double  %10.0g                Individuals Cross-sectional
*                                                Weight - without 1st wave
*                                                of a subsample
*w11103          double  %10.0g                Longitudinal Weight -
*                                                Respondent Individual
*w11105          double  %10.0g                Cross-sectional Weight - all samples


restore


**********************************************************************
***DETERMINANTS OF SWICHTING RATES (NO TABLE BUT MENTIONED IN TEXT)***
**********************************************************************
**********************
***Sample Selection***
**********************

*Note: while it may make sense to keep family enrollees in the switching rates above, we should not keep them here. 
*Also define sample: all those at least one in PHI*

drop if PrivateHIfamilyenrollee==1
drop if SHIfamilyenrollee==1
drop if SHI==. & PrivateHI==.
capture drop PHIsample

replace CurrentlyUnemployed=0 if CurrentlyUnemployed==.

sort pid
by pid: egen PHIsample=count(PrivateHI)


xi: xtreg topublic age CurrentlyUnemployed civilservant selfemployed whitecollar hospitalnights doctorvisits fulltimeemployed parttimeemployed jobtraining  Hauptschule Abi labgroII o_eqpostII i.syear if (PHIsample>0), cluster(pid) fe
est store casesOLSI
xi: xtreg topublic age CurrentlyUnemployed civilservant selfemployed whitecollar hospitalnights doctorvisits fulltimeemployed parttimeemployed jobtraining  Hauptschule Abi labgroII o_eqpostII i.syear if (PHIsample>0 & age>=25 & age<=95 & syear>1990), cluster(pid) fe
est store casesOLSII
outreg2 [casesOLSI casesOLSII] using "$tables\DeterminantsToPublic.xls", replace stats(coef se)  dec(4) excel

clear 
exit


